package com.twu.biblioteca;

import com.twu.bean.User;
import com.twu.option.HomeOption;
import com.twu.option.Option;

public class BibliotecaLibrary
{
	private static final String BACK_TO_HOME_COMMAND = "*";
	private static final String QUIT_COMMAND = "Q";
	private final Login login;
	private Option currentOption;
	private Option homeOption;

	public BibliotecaLibrary()
	{
		login = new Login();
	}

	public String start()
	{
		return "Welcome to Biblioteca online library";
	}

	public String executeMenuOption(String input)
	{
		try
		{
			int optionNumber = Integer.parseInt(input);
			setCurrentOption(optionNumber);
			return currentOption.execute();
		} catch (NumberFormatException e)
		{
			handleQuit(input);
			handleBackToHome(input);
			return "You can continue to select the option!";
		}
	}

	public String getTips()
	{
		return currentOption.getTips() + " (input q to quit system; * to back to home):";
	}

	public boolean isLogin(User inputUser)
	{
		return login.isLogin(inputUser);
	}

	public void login()
	{
		homeOption = new HomeOption(login.getCurrentUser());
		currentOption = homeOption;
	}

	public void guest()
	{
		homeOption = new HomeOption();
		currentOption = homeOption;
	}

	private void setCurrentOption(int optionNumber)
	{
		currentOption = currentOption.selectOption(optionNumber);
	}

	private void handleBackToHome(String input)
	{
		if (BACK_TO_HOME_COMMAND.equals(input))
		{
			currentOption = homeOption;
		}
	}

	private void handleQuit(String input)
	{
		if (QUIT_COMMAND.equals(input.toUpperCase()))
		{
			System.exit(0);
		}
	}
}
